<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 11/29/12
 * Time: 9:09 AM
 * To change this template use File | Settings | File Templates.
 */
class Controllers_Product extends Libs_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->model=new Models_Product();
        $this->view->listmenu=$this->model->getcatelogy();
    }
    public function index($param=null)
    {


        $this->view->cate=$param[0];
        $this->view->listpro=$this->model->getproduct($param[0]);
        $this->view->render('product/index');
        if($_POST['ItemDel']) {
            //$action='';
            if($_POST['element']=='') {

                echo '<script>alert(\'Mời bạn chọn ít nhất 1 sản phẩm\')</script>';
                return;
            }
            else{

                $id=implode(",",$_POST['element']);
                //echo $id;
                $this->model->delpro($id);


                echo '<script>location.reload();</script>';
            }
        }


    }
    public function detail($param=null)
    {

        $this->view->listpro=$this->model->prodetail($param[0]);
        $this->view->listmenu=$this->model->getmenupro();
        $this->view->listmafa=$this->model->getmanufactures();
        if($_REQUEST["sua"])
        {
            $masp=$param[0];
            $mahang=$_REQUEST["hangsx"];
            $maloai=$_REQUEST["loaisp"];
            $nhacungcap=$_REQUEST["nhacungcap"];
            $tensp=$_REQUEST["tensp"];
            $mota=$_REQUEST["motasp"];
            $soluong=$_REQUEST["soluongsp"];
            $gia=$_REQUEST["giasp"];
            $anh=$_REQUEST["anhsp"];
            $baohanh=$_REQUEST["baohanh"];

            $trangthaisp=$_REQUEST["trangthaisp"];
            $rs=$this->model->checkproduct($tensp,$masp);
            if(count($rs))
            {
                echo"<script>alert('San pham bi trung ten');</script>";
                $this->detail($masp);
            }
            else
            {
                if($anh=='')
                {
                    foreach($this->view->listpro as $key)
                    {
                        $anh=$key->picture;

                    }
                }

                $this->model->updatepro($masp,$mahang,$maloai,$nhacungcap,$tensp,$mota,$soluong,$gia,$anh,$baohanh,$trangthaisp);

                echo '<script>location.reload();</script>';
            }
        }
        else
        $this->view->render('product/detail');
    }
    public function addproduct()
    {
        $mahang=$_REQUEST["hangsx"];
        $maloai=$_REQUEST["loaisp"];
        $nhacungcap=$_REQUEST["nhacungcap"];
        $ngaynhap=date("Y-m-d");
        $tensp=$_REQUEST["tensp"];
        $mota=$_REQUEST["motasp"];
        $soluong=$_REQUEST["soluongsp"];
        $gia=$_REQUEST["giasp"];
        $anh=$_REQUEST["anh"];
        $baohanh=$_REQUEST["baohanh"];
        $trangthaisp=$_REQUEST["trangthaisp"];
        $this->view->listmenu=$this->model->getmenupro();
        $this->view->listmafa=$this->model->getmanufactures();
        $this->view->render('product/add');
        if($_REQUEST["them"])
        {
            $check=$this->model->checkproduct($tensp,0);
            if(count($check))
            {
                echo"<script>alert('Tên sản phẩm đã tồn tại. Đề nghị nhập một tên khác');reload(); onload=function(){frmthemsanpham.tensp.focus();}</script>";
            }
            else
            {
                $this->model->insertpro($mahang,$maloai,$nhacungcap,$ngaynhap,$tensp,$mota,$soluong,$gia,$anh,$baohanh,$trangthaisp);
                    echo"<script>if(!confirm('Thêm sản phẩm thành công. Bạn có muốn thêm sản phẩm nữa không?')) location='?options=sanpham';</script>";
            }
        }
    }
    public function search()
    {
        $this->view->listpro=$this->model->seachpro($_POST['pname']);
        $this->view->render('product/index');
        if($_POST['ItemDel']) {
            //$action='';
            if($_POST['element']=='') {

                echo '<script>alert(\'Mời bạn chọn ít nhất 1 sản phẩm\')</script>';
                return;
            }
            else{

                $id=implode(",",$_POST['element']);
                //echo $id;
                $this->model->delpro($id);


                echo '<script>location.reload();</script>';
            }
        }

    }
}
